<?php

class newprinter_task {

    function post_install($options){

        $this->post_update($optionss);
    }

    function post_update($params){

        set_time_limit(0);
        $db = kernel::database();
        $db_dly = app::get('ome')->model('delivery')->table_name(true);
        $db_dly_item = app::get('ome')->model('delivery_items')->table_name(true);
        $rows = $db->select('SELECT delivery_id FROM ' . $db_dly);

        foreach ($rows as $row) {
            $dId = $row['delivery_id'];
            $items = $db->select("SELECT bn,product_id,number FROM {$db_dly_item} where delivery_id={$dId}");
            if (is_array($items)) {

                $skuNum = count($items);
                $bns = array();
                $totalNum = 0;
                foreach ($items as $item) {
                    $totalNum += $item['number'];
                    $bns[$item['product_id']] = $item['bn'];
                }
                ksort($bns);
                $bnsContent = serialize($bns);
                $idx_split = $skuNum * 10000000000 + sprintf("%u", crc32($bnsContent));

            } else {

                $idx_split = 0;
                $bnsContent ='';
                $totalNum = 0;
            }

            $db->exec("UPDATE {$db_dly} SET idx_split='{$idx_split}', bnsContent='" . mysql_escape_string($bnsContent) . "' , itemNum='$totalNum', skuNum='$skuNum' WHERE delivery_id={$dId} ");
        }
    }
}